﻿namespace BM.Tools.WebControls.MForm.Generator
{
    using System.Linq;
    using System.Xml.Schema;

    /// <summary>
    /// A class representing the data of the <see cref="XmlSchemaChoice"/> class.
    /// </summary>
    public class FormChoice : FormItem<XmlSchemaChoice>
    {
        private int choiceIndex;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormChoice"/> class.
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="schemaObject">
        /// The schema object.
        /// </param>
        public FormChoice(FormBuilder builder, FormVisited parent, XmlSchemaChoice schemaObject) : base(builder, parent, schemaObject)
        {
        }

        /// <summary>
        /// Gets the index of this object, being an ordinal number of all <see cref="FormChoice" /> objects in its parent.
        /// </summary>
        public int ChoiceIndex
        {
            get
            {
                if (choiceIndex == 0)
                {
                    choiceIndex = Parent.Children.TakeWhile(child => child != this).Where(child => child is FormChoice).Count() + 1;
                }

                return choiceIndex;
            }
        }

        /// <summary>
        /// Gets qualified name text.
        /// </summary>        
        public override string QualifiedName
        {
            get
            {
                return "Ch" + ChoiceIndex;
            }
        }

        /// <summary>
        /// Gets MaxOccurs from schema.
        /// </summary>
        protected override decimal MaxOccursWithoutLimitations
        {
            get
            {
                return SchemaObject.MaxOccurs;
            }
        }

        /// <summary>
        /// Gets choice max occurs.
        /// </summary>
        protected override decimal SchemaObjectMaxOccurs
        {
            get
            {
                return SchemaObject.MaxOccurs;
            }
        }

        /// <summary>
        /// Gets choice min occurs.
        /// </summary>
        protected override decimal SchemaObjectMinOccurs
        {
            get
            {
                return SchemaObject.MinOccurs;
            }
        }
    }
}
